package com.jsoft.controller;

import com.jsoft.annotation.Captcha;
import com.jsoft.annotation.Repeat;
import com.jsoft.constant.Constants;
import com.jsoft.core.RedisTemplate;
import com.jsoft.annotation.Log;
import com.jsoft.entity.Information;
import com.jsoft.entity.LoginUser;
import com.jsoft.entity.User;
import com.jsoft.exception.BusinessException;
import com.jsoft.service.InformationService;
import com.jsoft.service.UserService;
import com.jsoft.util.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 基本操作类
 * <p>
 * 该类用于登录注册退出等基本操作
 */
@RestController

@RequestMapping("/indexUser")
public class IndexController {

    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private InformationService informationService;

    /**
     * 返回登陆结果
     *
     * @param user 用户
     * @return 返回用于登录的用户对象
     */
    @PostMapping("/login")
    public Result<LoginUser> login(@RequestBody User user) {

        LoginUser loginUser = userService.login(user);
        return Result.success(loginUser);
    }

    /**
     * 用户退出
     *
     * @return 返回退出信息
     */
    @PostMapping("/logout")
    public Result logout() {
        userService.logout();
        return Result.success("安全退出");
    }

    /**
     * 用户注册
     *
     * @param
     * @return
     */

    @Repeat(40)
    @PostMapping("/register")
    public Result register(@RequestBody User user) {

        userService.register(user);
        return Result.success("注册成功");
    }



    /**
     * 账户校验
     *
     * 对比电话和验证码
     *
     * @param userData 验证码等信息
     * @return 返回结果
     */

    @PostMapping("/confirmAccountByPhone")
    public Result confirmAccountByPhone(@RequestBody Map<String, Object> userData) {

        //提取用于账户确认的信息
        String phone = (String) userData.get("phone");
        String token = (String) userData.get("token");
        String captcha = (String) userData.get("captcha");
        String result = redisTemplate.get(Constants.CAPTCHA_PREFIX + token);
        //查询用户电话是否存在
        User user = userService.queryByPhone(phone);
        //验证码校验，匹配大小写
        if (result == null) {
            throw new BusinessException("验证码已过期", Code.GET_ERR);
        } else if (!result.equals(captcha.toLowerCase())) {
            throw new BusinessException("验证码错误", Code.GET_ERR);
        }
        return Result.success(user.getUsername());
    }

    /**
     * 安全认证
     *
     * 校验验证码
     *
     * @param userData 验证码信息
     * @return
     */
    @Captcha
    @PostMapping("/SecurityCertifications")
    public Result SecurityCertifications(@RequestBody Map<String, Object> userData) {
        return Result.success();
    }

    /**
     * 重置密码
     *
     * @param userData 密码和token
     * @return 返回结果
     */

    //@Log(title = "post",businessType = "找回密码")
    @Captcha
    @PostMapping("/resetPassword")
    @Repeat(40)
    public Result resetPassword(@RequestBody Map<String, Object> userData) {

        //获取信息用于更新密码
        String phone = (String) userData.get("phone");
        String password = (String) userData.get("password");
        //更新密码
        userService.updateByPhone(phone, password);
        return Result.success("成功修改密码");
    }


}
